package com.sskj.api.controller.admin;

import com.sskj.api.domain.Permission;
import com.sskj.api.service.IPermissionService;
import com.sskj.common.dto.TreeNode;
import com.sskj.common.util.ApiResponseUtil;
import com.sskj.common.vo.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "权限模块")
@RestController
@RequestMapping("/admin/permission")
@Validated
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @Operation(summary = "新增权限")
    @PostMapping("add")
    public ApiResponse<Object> add(@Valid @RequestBody Permission permission) {
        permissionService.add(permission);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "删除权限")
    @PostMapping("delete")
    public ApiResponse<Object> delete(@RequestParam Long id) {
        permissionService.deleteById(id);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新权限")
    @PostMapping("update")
    public ApiResponse<Object> update(@Valid @RequestBody Permission permission) {
        permissionService.update(permission);

        return ApiResponseUtil.success();
    }

    @Operation(summary = "权限树结构")
    @GetMapping("tree")
    public ApiResponse<List<TreeNode<Permission>>> tree() {
        List<TreeNode<Permission>> tree = permissionService.tree(-1);

        return ApiResponseUtil.success(tree);
    }
}
